package com.a4studio.android.newsreader;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.TextView;

public class NewsDetailActivity extends Activity {
	
	private TextView source;
	private TextView date;
	private TextView clickUrl;
	private TextView content;
	
	private String url;
	
	public final static String SOURCE_BUNDLE_KEY = "NEWS_DETAIL_SOURCE";
	public final static String DATE_BUNDLE_KEY = "NEWS_DETAIL_DATE";
	public final static String CLICKURL_BUNDLE_KEY = "NEWS_DETAIL_CLICKURL";
	public final static String CONTENT_BUNDLE_KEY = "NEWS_DETAIL_CONTENT";
	
	private final SimpleDateFormat DATE_FORMAT=new SimpleDateFormat("yyyy-MM-dd");
	private final SimpleDateFormat TIME_FORMAT=new SimpleDateFormat("HH:mm");
	private Calendar cal = Calendar.getInstance();
	
	public NewsDetailActivity()
	{
		
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_details);
		
		source = (TextView)findViewById(R.id.news_detail_source);
		date = (TextView)findViewById(R.id.news_detail_date);
		clickUrl = (TextView)findViewById(R.id.news_detail_clickurl);
		content = (TextView)findViewById(R.id.news_detail_content);
		//content.setMovementMethod(ScrollingMovementMethod.getInstance());
		
		Bundle bundle = this.getIntent().getExtras();
		source.setText(bundle.getString(SOURCE_BUNDLE_KEY));
		
		
		long unixTimeinMillis = bundle.getLong(DATE_BUNDLE_KEY)*1000;
		cal.setTimeInMillis(unixTimeinMillis);
		String pubDate = "";
		if(cal.before(new Date()))
		{
			pubDate = DATE_FORMAT.format(new Date(unixTimeinMillis));
		}
		{
			pubDate = TIME_FORMAT.format(new Date(unixTimeinMillis));
		}
		date.setText(pubDate);
		
		url = bundle.getString(CLICKURL_BUNDLE_KEY);
		clickUrl.setText(url);
		clickUrl.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
                startActivity(new Intent(
                        Intent.ACTION_VIEW, Uri.parse(url)));
			}
			
		});
		
		content.setText(bundle.getString(CONTENT_BUNDLE_KEY));
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
	

}
